<template>
    <div class="wh-100" style="background-color: #fff">
        <!--title&添加场地-->
        <div class="d-flex-between-center px-12" style="height: 60px; border-bottom: 1px solid #eef1f7">
            <div class="fs-16 fw-7">添加校区</div>
            <Button class="fs-12" @click="$router.back()">返回</Button>
        </div>
        <!--表单-->
        <div class="ml-68 mt-16">
            <Form :label-width="110" class="mt-20" ref="addCampusRef" :model="addCampusForm" :rules="addCampusRules">
                <FormItem style="width: 400px" label="校区名称" prop="campusName">
                    <Input v-model="addCampusForm.campusName" placeholder="输入校区名称"></Input>
                </FormItem>
                <FormItem label="是否启用" prop="currentStatus">
                    <i-switch v-model="addCampusForm.currentStatus" />
                </FormItem>
                <FormItem style="width: 400px" label="详细地址" prop="campusAddress">
                    <Input v-model="addCampusForm.campusAddress" placeholder="填写详细校区详细地址"></Input>
                </FormItem>
                <FormItem>
                    <Button style="width: 100px" type="primary" @click="submitForm">保存</Button>
                </FormItem>
            </Form>
        </div>
    </div>
</template>
<script>
import { addCampusAPI } from '../../../api/bys/siteAdministration/index';
// region TODO 表单
const addCampusForm = {
    campusName: '',
    currentStatus: false,
    campusAddress: ''
};
const addCampusRules = {
    campusName: [{ required: true, message: '必填项', trigger: 'blur' }],
    campusAddress: [{ required: true, message: '必填项', trigger: 'blur' }]
};
// endregion
export default {
    data() {
        return {
            addCampusForm,
            addCampusRules
        };
    },
    methods: {
        submitForm() {
            this.$refs.addCampusRef.validate(valid => {
                if (valid) {
                    this.$confirm('你确定添加该校区吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    })
                        .then(() => {
                            addCampusAPI(this.addCampusForm).then(res => {
                                if (res.success) {
                                    this.$message({
                                        type: 'success',
                                        message: '添加校区成功!'
                                    });
                                    this.$router.back();
                                }
                            });
                        })
                        .catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消添加'
                            });
                        });
                } else {
                    this.$message.error('请完善相关信息！');
                    return false;
                }
            });
        }
    }
};
</script>
